
Imports std

Namespace language
    Namespace japanese

        Public Module ___kana2pron___

            'Protected Friend Enum charType
            '    hira = 0
            '    kata = 1
            '    other = 2
            'End enum

            'Protected Friend Function checkCharType(ByVal c As Char) As charType
            '    If iskata(c) Then
            '        Return charType.kata
            '    ElseIf ishira(c) Then
            '        Return charType.hira
            '    Else
            '        Return charType.other
            '    End If
            'End Function

            Public Const vowel As String = "aiueo"

            Public ReadOnly shorten(,) As String = { _
                            {shortenformat("i") + shortenformat("le"), shortenformat("ye")}, _
 _
                            {shortenformat("u") + shortenformat("la"), shortenformat("wha")}, _
                            {shortenformat("u") + shortenformat("li"), shortenformat("whi")}, _
                            {shortenformat("u") + shortenformat("le"), shortenformat("whe")}, _
                            {shortenformat("u") + shortenformat("lo"), shortenformat("who")}, _
 _
                            {shortenformat("ki") + shortenformat("lya"), shortenformat("kya")}, _
                            {shortenformat("ki") + shortenformat("li"), shortenformat("kyi")}, _
                            {shortenformat("ki") + shortenformat("lyu"), shortenformat("kyu")}, _
                            {shortenformat("ki") + shortenformat("le"), shortenformat("kye")}, _
                            {shortenformat("ki") + shortenformat("lyo"), shortenformat("kyo")}, _
 _
                            {shortenformat("ku") + shortenformat("lya"), shortenformat("qya")}, _
                            {shortenformat("ku") + shortenformat("lyu"), shortenformat("qyu")}, _
                            {shortenformat("ku") + shortenformat("lyo"), shortenformat("qyo")}, _
 _
                            {shortenformat("ku") + shortenformat("la"), shortenformat("qwa")}, _
                            {shortenformat("ku") + shortenformat("li"), shortenformat("qwi")}, _
                            {shortenformat("ku") + shortenformat("lu"), shortenformat("qwu")}, _
                            {shortenformat("ku") + shortenformat("le"), shortenformat("qwe")}, _
                            {shortenformat("ku") + shortenformat("lo"), shortenformat("qwo")}, _
 _
                            {shortenformat("gu") + shortenformat("la"), shortenformat("gya")}, _
                            {shortenformat("gu") + shortenformat("li"), shortenformat("gyi")}, _
 _
                            {shortenformat("gi") + shortenformat("lyu"), shortenformat("gyu")}, _
                            {shortenformat("gi") + shortenformat("le"), shortenformat("gye")}, _
                            {shortenformat("gi") + shortenformat("lyo"), shortenformat("gyo")}, _
 _
                            {shortenformat("gu") + shortenformat("la"), shortenformat("gwa")}, _
                            {shortenformat("gu") + shortenformat("li"), shortenformat("gwi")}, _
                            {shortenformat("gu") + shortenformat("lu"), shortenformat("gwu")}, _
                            {shortenformat("gu") + shortenformat("le"), shortenformat("gwe")}, _
                            {shortenformat("gu") + shortenformat("lo"), shortenformat("gwo")}, _
 _
                            {shortenformat("si") + shortenformat("lya"), shortenformat("sya")}, _
                            {shortenformat("si") + shortenformat("li"), shortenformat("syi")}, _
                            {shortenformat("si") + shortenformat("lyu"), shortenformat("syu")}, _
                            {shortenformat("si") + shortenformat("le"), shortenformat("sye")}, _
                            {shortenformat("si") + shortenformat("lyo"), shortenformat("syo")}, _
 _
                            {shortenformat("su") + shortenformat("la"), shortenformat("swa")}, _
                            {shortenformat("su") + shortenformat("li"), shortenformat("swi")}, _
                            {shortenformat("su") + shortenformat("lu"), shortenformat("swu")}, _
                            {shortenformat("su") + shortenformat("le"), shortenformat("swe")}, _
                            {shortenformat("su") + shortenformat("lo"), shortenformat("swo")}, _
 _
                            {shortenformat("zi") + shortenformat("lya"), shortenformat("zya")}, _
                            {shortenformat("zi") + shortenformat("li"), shortenformat("zyi")}, _
                            {shortenformat("zi") + shortenformat("lyu"), shortenformat("zyu")}, _
                            {shortenformat("zi") + shortenformat("le"), shortenformat("zye")}, _
                            {shortenformat("zi") + shortenformat("lyo"), shortenformat("zyo")}, _
 _
                            {shortenformat("ti") + shortenformat("lya"), shortenformat("tya")}, _
                            {shortenformat("ti") + shortenformat("li"), shortenformat("tyi")}, _
                            {shortenformat("ti") + shortenformat("lyu"), shortenformat("tyu")}, _
                            {shortenformat("ti") + shortenformat("le"), shortenformat("tye")}, _
                            {shortenformat("ti") + shortenformat("lyo"), shortenformat("tyo")}, _
 _
                            {shortenformat("tu") + shortenformat("la"), shortenformat("tsa")}, _
                            {shortenformat("tu") + shortenformat("li"), shortenformat("tsi")}, _
                            {shortenformat("tu") + shortenformat("le"), shortenformat("tse")}, _
                            {shortenformat("tu") + shortenformat("lo"), shortenformat("tso")}, _
 _
                            {shortenformat("te") + shortenformat("lya"), shortenformat("tha")}, _
                            {shortenformat("te") + shortenformat("li"), shortenformat("thi")}, _
                            {shortenformat("te") + shortenformat("lyu"), shortenformat("thu")}, _
                            {shortenformat("te") + shortenformat("le"), shortenformat("the")}, _
                            {shortenformat("te") + shortenformat("lyo"), shortenformat("tho")}, _
 _
                            {shortenformat("di") + shortenformat("lya"), shortenformat("dya")}, _
                            {shortenformat("di") + shortenformat("li"), shortenformat("dyi")}, _
                            {shortenformat("di") + shortenformat("lyu"), shortenformat("dyu")}, _
                            {shortenformat("di") + shortenformat("le"), shortenformat("dye")}, _
                            {shortenformat("di") + shortenformat("lyo"), shortenformat("dyo")}, _
 _
                            {shortenformat("de") + shortenformat("lya"), shortenformat("dha")}, _
                            {shortenformat("de") + shortenformat("li"), shortenformat("dhi")}, _
                            {shortenformat("de") + shortenformat("lyu"), shortenformat("dhu")}, _
                            {shortenformat("de") + shortenformat("le"), shortenformat("dhe")}, _
                            {shortenformat("de") + shortenformat("lyo"), shortenformat("dho")}, _
 _
                            {shortenformat("do") + shortenformat("la"), shortenformat("dwa")}, _
                            {shortenformat("do") + shortenformat("li"), shortenformat("dwi")}, _
                            {shortenformat("do") + shortenformat("lu"), shortenformat("dwu")}, _
                            {shortenformat("do") + shortenformat("le"), shortenformat("dwe")}, _
                            {shortenformat("do") + shortenformat("lo"), shortenformat("dwo")}, _
 _
                            {shortenformat("ni") + shortenformat("lya"), shortenformat("nya")}, _
                            {shortenformat("ni") + shortenformat("li"), shortenformat("nyi")}, _
                            {shortenformat("ni") + shortenformat("lyu"), shortenformat("nyu")}, _
                            {shortenformat("ni") + shortenformat("le"), shortenformat("nye")}, _
                            {shortenformat("ni") + shortenformat("lyo"), shortenformat("nyo")}, _
 _
                            {shortenformat("hi") + shortenformat("lya"), shortenformat("hya")}, _
                            {shortenformat("hi") + shortenformat("li"), shortenformat("hyi")}, _
                            {shortenformat("hi") + shortenformat("lyu"), shortenformat("hyu")}, _
                            {shortenformat("hi") + shortenformat("le"), shortenformat("hye")}, _
                            {shortenformat("hi") + shortenformat("lyo"), shortenformat("hyo")}, _
 _
                            {shortenformat("hu") + shortenformat("lya"), shortenformat("fya")}, _
                            {shortenformat("hu") + shortenformat("lyu"), shortenformat("fyu")}, _
                            {shortenformat("hu") + shortenformat("lyo"), shortenformat("fyo")}, _
 _
                            {shortenformat("hu") + shortenformat("la"), shortenformat("fwa")}, _
                            {shortenformat("hu") + shortenformat("li"), shortenformat("fwi")}, _
                            {shortenformat("hu") + shortenformat("lu"), shortenformat("fwu")}, _
                            {shortenformat("hu") + shortenformat("le"), shortenformat("fwe")}, _
                            {shortenformat("hu") + shortenformat("lo"), shortenformat("fwo")}, _
 _
                            {shortenformat("bi") + shortenformat("lya"), shortenformat("bya")}, _
                            {shortenformat("bi") + shortenformat("li"), shortenformat("byi")}, _
                            {shortenformat("bi") + shortenformat("lyu"), shortenformat("byu")}, _
                            {shortenformat("bi") + shortenformat("le"), shortenformat("bye")}, _
                            {shortenformat("bi") + shortenformat("lyo"), shortenformat("byo")}, _
 _
                            {shortenformat("vu") + shortenformat("la"), shortenformat("va")}, _
                            {shortenformat("vu") + shortenformat("li"), shortenformat("vi")}, _
                            {shortenformat("vu") + shortenformat("le"), shortenformat("ve")}, _
                            {shortenformat("vu") + shortenformat("lo"), shortenformat("vo")}, _
 _
                            {shortenformat("vu") + shortenformat("lya"), shortenformat("vya")}, _
                            {shortenformat("vu") + shortenformat("lyu"), shortenformat("vyu")}, _
                            {shortenformat("vu") + shortenformat("lyo"), shortenformat("vyo")}, _
 _
                            {shortenformat("pi") + shortenformat("lya"), shortenformat("pya")}, _
                            {shortenformat("pi") + shortenformat("li"), shortenformat("pyi")}, _
                            {shortenformat("pi") + shortenformat("lyu"), shortenformat("pyu")}, _
                            {shortenformat("pi") + shortenformat("le"), shortenformat("pye")}, _
                            {shortenformat("pi") + shortenformat("lyo"), shortenformat("pyo")}, _
 _
                            {shortenformat("mi") + shortenformat("lya"), shortenformat("mya")}, _
                            {shortenformat("mi") + shortenformat("li"), shortenformat("myi")}, _
                            {shortenformat("mi") + shortenformat("lyu"), shortenformat("myu")}, _
                            {shortenformat("mi") + shortenformat("le"), shortenformat("mye")}, _
                            {shortenformat("mi") + shortenformat("lyo"), shortenformat("myo")}, _
 _
                            {shortenformat("ri") + shortenformat("lya"), shortenformat("rya")}, _
                            {shortenformat("ri") + shortenformat("li"), shortenformat("ryi")}, _
                            {shortenformat("ri") + shortenformat("lyu"), shortenformat("ryu")}, _
                            {shortenformat("ri") + shortenformat("le"), shortenformat("rye")}, _
                            {shortenformat("ri") + shortenformat("lyo"), shortenformat("ryo")}}

            Public ReadOnly shorten2(,) As String = { _
                            {shortenformat("whi"), shortenformat("wi")}, _
                            {shortenformat("whe"), shortenformat("we")}, _
 _
                            {shortenformat("qwa"), shortenformat("qa")}, _
                            {shortenformat("qwi"), shortenformat("qi")}, _
                            {shortenformat("qwe"), shortenformat("qe")}, _
                            {shortenformat("qwo"), shortenformat("qo")}, _
 _
                            {shortenformat("zya"), shortenformat("ja")}, _
                            {shortenformat("zyu"), shortenformat("ju")}, _
                            {shortenformat("zye"), shortenformat("je")}, _
                            {shortenformat("zyo"), shortenformat("jo")}, _
 _
                            {shortenformat("fwi"), shortenformat("fi")}, _
                            {shortenformat("fwe"), shortenformat("fe")}, _
                            {shortenformat("fwo"), shortenformat("fo")}}

            Public Class mark
                Public Shared ReadOnly wrong As String = character.asterisk
                Public Shared ReadOnly shortenSeperator As String = character.blank
            End Class

            Public Function shortenformat(ByRef input As String) As String
                Return mark.shortenSeperator + input + mark.shortenSeperator
            End Function

            Public Sub pronshorten(ByRef input As String, ByVal level As Int32)
                Dim i As Int32
                Dim rule As String(,) = Nothing
                Dim tmp As String = Nothing
                Dim index As Int32
                Dim seperatorLen As Int32 = strlen(mark.shortenSeperator)

                If level >= 2 Then
                    rule = shorten2
                ElseIf level <= 1 Then
                    rule = shorten
                End If

                If Not rule Is Nothing Then
                    For i = 0 To rule.GetLength(0) - 1
                        input = input.Replace(rule(i, 0), rule(i, 1))
                    Next

                    If level <= 1 Then
                        tmp = shortenformat("ltu")
                        i = input.IndexOf(tmp)
                        index = i + strlen(tmp) + seperatorLen
                        While index < strlen(input) And i >= 0
                            If input(index) <> "n" And input(index) <> "l" Then
                                input = strleft(input, i) + input(index) + strmid(input, index)
                            End If
                            i = input.IndexOf(tmp, i + 1)
                            index = i + strlen(tmp) + seperatorLen
                        End While
                    ElseIf level >= 2 Then
                        tmp = shortenformat("nn")
                        i = input.IndexOf(tmp)
                        index = i + strlen(tmp) + seperatorLen
                        While index < strlen(input) And i >= 0
                            If vowel.IndexOf(input(index)) = -1 Then
                                input = input.Remove(i + seperatorLen, 1)
                            End If
                            i = input.IndexOf(tmp, i + 1)
                            index = i + strlen(tmp) + seperatorLen
                        End While
                    End If
                End If
            End Sub

            Public Function kana2pron(ByRef str As String, Optional ByVal shortenlevel As Int32 = 2) As String
                Dim rtn As String = Nothing
                'Dim i As Int32
                'Dim lastCharType As charType = charType.other
                'Dim thisCharType As charType = charType.other
                'Dim lastIndex As Int32 = 0
                'For i = 0 To strlen(str) - 1
                '    thisCharType = checkCharType(str(i))
                '    If lastCharType <> thisCharType Then
                '        If lastCharType = charType.hira Then
                '            rtn += hira2pron(str.Substring(lastIndex, i - lastIndex), shortenlevel)
                '        ElseIf lastCharType = charType.kata Then
                '            rtn += kata2pron(str.Substring(lastIndex, i - lastIndex), shortenlevel)
                '        Else
                '            rtn += strmid(str, lastIndex, i - lastIndex)
                '        End If
                '        lastIndex = i
                '        lastCharType = thisCharType
                '    End If
                'Next
                'If lastIndex < strlen(str) Then
                '    If lastCharType = charType.hira Then
                '        rtn += hira2pron(str.Substring(lastIndex, i - lastIndex), shortenlevel)
                '    ElseIf lastCharType = charType.kata Then
                '        rtn += kata2pron(str.Substring(lastIndex, i - lastIndex), shortenlevel)
                '    Else
                '        rtn += strmid(str, lastIndex, i - lastIndex)
                '    End If
                'End If
                rtn = hira2pron(str, shortenlevel)
                rtn = kata2pron(rtn, shortenlevel)

                Return rtn
            End Function

            Public Sub cleanseperator(ByRef input As String)
                input = input.Replace(mark.shortenSeperator, emptyString)
            End Sub

            Public Sub fixlongpron(ByRef input As String)
                Dim index As Int32
                Dim i As Int32
                Dim len As Int32 = strlen(input)


                For i = 0 To longpron.Length() - 1
                    index = input.IndexOf(longpron(i))
                    While index < len AndAlso index > 0 AndAlso language.english.isalpha(input(index - 1))
                        strrplc(input, index, input(index - 1))
                        index = input.IndexOf(longpron(i), index + 1)
                    End While
                Next
            End Sub
        End Module

    End Namespace
End Namespace
